package com.momo.todo.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.momo.todo.R;
import com.momo.todo.model.HistoryModel;
import com.momo.todo.model.PlanModel;
import com.momo.todo.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/27.
 */

public class HistoryListAdapter extends BaseAdapter {

    private List<HistoryModel> hisitoryList;

    private Context context;
    private LayoutInflater inflater;

    public HistoryListAdapter(Context context,List<HistoryModel> hisitoryList) {
        this.hisitoryList = hisitoryList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return hisitoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return hisitoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    class ViewHolder{
        TextView tv_time;
        TextView tv_content;
        TextView tv_week;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {



        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_history_list,null);

            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.tv_week = (TextView) convertView.findViewById(R.id.tv_week);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        HistoryModel history = hisitoryList.get(position);
//
        ArrayList<PlanModel> planList2 = (ArrayList<PlanModel>) history.getPlanList();
//        sortList(planList2);

        SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
        for(int i=0;i<planList2.size();i++){
            PlanModel plan = planList2.get(i);
            if(plan.getType() == PlanModel.PLAN_TYPE_LIFE){
                SpannableString spanString = new SpannableString("• " + plan.getContent()+ "\n");
                ForegroundColorSpan lightBlueSpan = new ForegroundColorSpan(
                        context.getResources().getColor(R.color.darkBlue));
                spanString.setSpan(lightBlueSpan,0,spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanBuilder.append(spanString);
            } else if (plan.getType() == PlanModel.PLAN_TYPE_STUDY) {
                SpannableString spanString = new SpannableString("• " + plan.getContent()+ "\n");
                ForegroundColorSpan lightBlueSpan = new ForegroundColorSpan(
                        context.getResources().getColor(R.color.darkPink));
                spanString.setSpan(lightBlueSpan,0,spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanBuilder.append(spanString);
            } else{
                SpannableString spanString = new SpannableString("• " + plan.getContent()+ "\n");
                ForegroundColorSpan lightBlueSpan = new ForegroundColorSpan(
                        context.getResources().getColor(R.color.darkGreen));
                spanString.setSpan(lightBlueSpan,0,spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spanBuilder.append(spanString);
            }
        }
        holder.tv_content.setText(spanBuilder);

//        LogUtils.log(TimeUtils.timeStamp2Date(history.getTime()));
//        LogUtils.log(TimeUtils.getDay(history.getTime())+"");

        long historyTime = history.getTime();
        long currTime = System.currentTimeMillis();


        SpannableString timeSpanString;
        if(TimeUtils.isSameDay(history.getTime(),currTime)){
            timeSpanString = new SpannableString("今天");
        }else if(TimeUtils.isYesterDay(currTime,historyTime)){
            timeSpanString = new SpannableString("昨天");
        }else{
            if(TimeUtils.getDay(historyTime)<10){
                timeSpanString = new SpannableString(
                        "0"+ TimeUtils.getDay(historyTime)+"" +TimeUtils.getMonth(history.getTime())+"月");
            }else {
                timeSpanString = new SpannableString(
                        TimeUtils.getDay(history.getTime()) + "" + TimeUtils.getMonth(history.getTime()) + "月");
            }
        }

//        LogUtils.log(TimeUtils.getDay(history.getTime())+"日" +TimeUtils.getMonth(history.getTime())+"月");
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(80);
        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);

        timeSpanString.setSpan(sizeSpan,0,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        timeSpanString.setSpan(styleSpan,0,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.tv_time.setText(timeSpanString);

        String week = TimeUtils.getWeek(historyTime);
        holder.tv_week.setText(week);

        return convertView;
    }

    public void refresh(ArrayList<HistoryModel> list){
        this.hisitoryList = list;

        notifyDataSetChanged();
    }


}
